<template>
  <div>
    <h1>上方组件的求和为{{sum}}</h1>
    <h1>列表中第一个人的名字是{{firstPersonName}}</h1>
    <input type="text" placeholder="请输入名字" v-model="name">
    <button @click="add">添加</button>
    <button @click="firstNameWang">添加一个姓王的人</button>
    <button @click="addSentence">添加一句话</button>
    <ul>
      <li v-for="p in personList" :key="p.id">{{ p.name }}</li>
    </ul>
  </div>
</template>

<script>
//唯一的字符串
import {nanoid} from "nanoid";

export default {
  name: "Person",
  description: '',
  mixins: [],
  components: {},
  props: {},
  data() {
    return {
      name: ''
    }
  },
  computed: {
    personList() {
      return this.$store.state.personOptions.personList
    },
    sum(){
      return this.$store.state.countOptions.sum
    },
    firstPersonName(){
      return this.$store.getters['personOptions/firstPersonName']
    }
  },
  watch: {},
  filters: {},
  created() {
  },
  mounted() {
  },
  destroyed() {
  },
  methods: {
    add() {
      const personObj = {id: nanoid(), name: this.name}
      this.$store.commit('personOptions/ADD_PERSON', personObj)
      this.name = ''
    },
    firstNameWang(){
      const personObj = {id: nanoid(), name: this.name}
      this.$store.dispatch('personOptions/addPersonWang',personObj)
      this.name = ''
    },
    addSentence(){
      this.$store.dispatch('personOptions/addSentence')
    }

  }
}
</script>

<style scoped>

</style>